package com.ymatou.iapi.login.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.common.lang.CipherUtil;
import com.ymatou.iapi.login.parameter.MobileLoginBean;
import com.ymatou.iapi.login.parameter.WxUserLoginBean;
import com.ymatou.iapi.login.service.MobileLoginCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymatou.iapi.userservice.parameter.enums.AppSourceEnum;
import com.ymatou.iapi.userservice.parameter.enums.TerminalSourceEnum;
import com.ymatou.iapi.userservice.parameter.req.CreateUserReq;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.model.LoginLog;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.model.Userinfoex;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.utils.TestDataManager;
/**
*手机登入
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_MobileLogin{
	private static MobileLoginBean mobileloginBean;
	private static MobileLoginCall mobileloginCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("手机登入");
	}

	@Before
	public void caseUp() {
		mobileloginBean=new MobileLoginBean();
		mobileloginCall=new MobileLoginCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(mobileloginCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyDB(Integer uid, MobileLoginBean mobileloginBean2) throws Exception {
		JUserWapper ju = new JUserWapper();
		Logger.comment("验证Userinfo");
		List<Userinfo> lui = ju.selectUserinfoByUid(uid);
		Logger.verifyEquals(1, lui.size(), "验证记录条数");
		Userinfo ui = lui.get(0);
		Logger.verifyNotNull(ui.getLoginId(), "验证LoginId");
		Logger.verifyEquals(mobileloginBean2.getMobile()==null?"":mobileloginBean2.getMobile(), ui.getLoginMobile(), "验证LoginMobile");
		Logger.verifyEquals(0, ui.getUserType(), "验证UserType");
		Logger.verifyEquals(0, ui.getUserState(), "验证UserState");
		Logger.verifyEquals(1, ui.getUserLevel(), "验证Userlevel");
		if(mobileloginBean2.getMobile()!=null&&mobileloginBean2.getMobile().length()>0&&JUserCallService.OpenEncrypt){
			String encryptMobile=CipherUtil.encryptAES(JUserCallService.EncryptKey, mobileloginBean2.getMobile());
			Logger.verifyEquals(encryptMobile, ui.getEncryptedLoginMobile(), "验证加密手机");
		}
		Logger.comment("验证Userinfoex");
		List<Userinfoex> luiex = ju.selectUserinfoexByUid(uid);
		Logger.verifyEquals(1, luiex.size(), "验证记录条数");
		Userinfoex uiex = luiex.get(0);
		Logger.verifyEquals(0, uiex.getChannelSource(), "验证ChannelSource");
		Logger.verifyEquals(mobileloginBean2.getAppSource(), uiex.getRegSource(), "验证RegSource");
		if (mobileloginBean2.getTerminalSource() != null) {
			Logger.verifyEquals(mobileloginBean2.getTerminalSource(), uiex.getTerminalSource(), "验证TerminalSource");
		}
		Logger.verifyEquals(mobileloginBean2.getDid(), uiex.getEquimentid(), "验证Equimentid");
		Logger.verifyEquals(mobileloginBean2.getDeviceId(), uiex.getDeviceId(), "验证DeviceId");
		Logger.verifyEquals(mobileloginBean2.getClientIp(), uiex.getClientIp(), "验证ClientIp");
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_MobileLogin_001() {
		Logger.start(true,"手机登入global,注册成功");
		try {
			String moblie = TestDataManager.getPhoneNum();
			if (JUserCallService.mobileExists(moblie)) {
				moblie = TestDataManager.getPhoneNum();
			}
			mobileloginBean.setAppSource(AppSourceEnum.GlobalApp.getCode());
			mobileloginBean.setClientIp(TestDataManager.getRandomIp());
			mobileloginBean.setClientPort("177");
			mobileloginBean.setDeviceId(TestDataManager.getRandomUUID().toString());
			mobileloginBean.setDid(TestDataManager.getRandomUUID().toString());
			mobileloginBean.setMobile(moblie);
			mobileloginBean.setTerminalSource(TerminalSourceEnum.IOS.getCode());
			mobileloginCall.setData(mobileloginBean);
			mobileloginCall.callService();			
			Logger.verifyEquals("200", mobileloginCall.getString("code"), "验证code");
			int uid=mobileloginCall.getJsonObject().get("data").getAsJsonObject().get("userId").getAsInt();
			verifyDB(uid, mobileloginBean);
			LoginLog loginLog = new JUserWapper().selectLastLoginLogByUid(uid).get(0);
			Logger.debug(FormatUtil.beanToGSONString(loginLog));
			Logger.verifyEquals(mobileloginBean.getClientPort(), loginLog.getClientPort(), "验证LoginLog表中getClientPort");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_MobileLogin_002() {
		Logger.start(true,"手机登入global,登入成功");
		try {
			String moblie = TestDataManager.getPhoneNum();
			if (JUserCallService.mobileExists(moblie)) {
				moblie = TestDataManager.getPhoneNum();
			}
			mobileloginBean.setAppSource(AppSourceEnum.GlobalApp.getCode());
			mobileloginBean.setClientIp(TestDataManager.getRandomIp());
			mobileloginBean.setDeviceId(TestDataManager.getRandomUUID().toString());
			mobileloginBean.setDid(TestDataManager.getRandomUUID().toString());
			mobileloginBean.setMobile(moblie);
			mobileloginBean.setTerminalSource(TerminalSourceEnum.IOS.getCode());
			mobileloginCall.setData(mobileloginBean);
			mobileloginCall.callService();			
			mobileloginCall.callService();			
			Logger.verifyEquals("200", mobileloginCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_MobileLogin_003() {
		Logger.start(true,"手机登入码头已有账号成功,新token");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			mobileloginBean.setAppSource(AppSourceEnum.GlobalApp.getCode());
			mobileloginBean.setClientIp(TestDataManager.getRandomIp());
			mobileloginBean.setDeviceId(TestDataManager.getRandomUUID().toString());
			mobileloginBean.setDid(TestDataManager.getRandomUUID().toString());
			mobileloginBean.setMobile(regbean.getLoginMobile());
			mobileloginBean.setTerminalSource(TerminalSourceEnum.IOS.getCode());
			mobileloginCall.setData(mobileloginBean);
			mobileloginCall.callService();			
			Logger.verifyEquals("200", mobileloginCall.getString("code"), "验证code");
			String token2=mobileloginCall.getJsonObject().get("data").getAsJsonObject().get("token").getAsString();
			Logger.verifyNotEquals(token, token2, "验证token不同");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_MobileLogin_004() {
		Logger.start(true,"不输入手机号失败");
		try {
			mobileloginBean.setAppSource(AppSourceEnum.GlobalApp.getCode());
			mobileloginBean.setClientIp(TestDataManager.getRandomIp());
			mobileloginBean.setDeviceId(TestDataManager.getRandomUUID().toString());
			mobileloginBean.setDid(TestDataManager.getRandomUUID().toString());
//			mobileloginBean.setMobile(regbean.getLoginMobile());
			mobileloginBean.setTerminalSource(TerminalSourceEnum.IOS.getCode());
			mobileloginCall.setData(mobileloginBean);
			mobileloginCall.callService();			
			Logger.verifyEquals("400", mobileloginCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_MobileLogin_005() {
		Logger.start(true,"手机登入码头已有账号成功,设备号一致,新token");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			mobileloginBean.setAppSource(AppSourceEnum.GlobalApp.getCode());
			mobileloginBean.setClientIp(regbean.getClientIp());
			mobileloginBean.setDeviceId(regbean.getDeviceId());
			mobileloginBean.setDid(regbean.getEquimentid());
			mobileloginBean.setMobile(regbean.getLoginMobile());
			mobileloginBean.setTerminalSource(TerminalSourceEnum.IOS.getCode());
			mobileloginCall.setData(mobileloginBean);
			mobileloginCall.callService();			
			Logger.verifyEquals("200", mobileloginCall.getString("code"), "验证code");
			String token2=mobileloginCall.getJsonObject().get("data").getAsJsonObject().get("token").getAsString();
			Logger.verifyNotEquals(token, token2, "验证token不同");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_MobileLogin_006() {
		Logger.start(true,"不输入AppSource失败");
		try {
			String moblie = TestDataManager.getPhoneNum();
			if (JUserCallService.mobileExists(moblie)) {
				moblie = TestDataManager.getPhoneNum();
			}
//			mobileloginBean.setAppSource(AppSourceEnum.GlobalApp.getCode());
			mobileloginBean.setClientIp(TestDataManager.getRandomIp());
			mobileloginBean.setDeviceId(TestDataManager.getRandomUUID().toString());
			mobileloginBean.setDid(TestDataManager.getRandomUUID().toString());
			mobileloginBean.setMobile(moblie);
			mobileloginBean.setTerminalSource(TerminalSourceEnum.IOS.getCode());
			mobileloginCall.setData(mobileloginBean);
			mobileloginCall.callService();			
			Logger.verifyEquals("400", mobileloginCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_MobileLogin_007() {
		Logger.start(true,"不输入ClientIp失败");
		try {
			String moblie = TestDataManager.getPhoneNum();
			if (JUserCallService.mobileExists(moblie)) {
				moblie = TestDataManager.getPhoneNum();
			}
			mobileloginBean.setAppSource(AppSourceEnum.GlobalApp.getCode());
//			mobileloginBean.setClientIp(TestDataManager.getRandomIp());
			mobileloginBean.setDeviceId(TestDataManager.getRandomUUID().toString());
			mobileloginBean.setDid(TestDataManager.getRandomUUID().toString());
			mobileloginBean.setMobile(moblie);
			mobileloginBean.setTerminalSource(TerminalSourceEnum.IOS.getCode());
			mobileloginCall.setData(mobileloginBean);
			mobileloginCall.callService();			
			Logger.verifyEquals("400", mobileloginCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_MobileLogin_008() {
		Logger.start(true,"不输入DeviceId失败");
		try {
			String moblie = TestDataManager.getPhoneNum();
			if (JUserCallService.mobileExists(moblie)) {
				moblie = TestDataManager.getPhoneNum();
			}
			mobileloginBean.setAppSource(AppSourceEnum.GlobalApp.getCode());
			mobileloginBean.setClientIp(TestDataManager.getRandomIp());
//			mobileloginBean.setDeviceId(TestDataManager.getRandomUUID().toString());
			mobileloginBean.setDid(TestDataManager.getRandomUUID().toString());
			mobileloginBean.setMobile(moblie);
			mobileloginBean.setTerminalSource(TerminalSourceEnum.IOS.getCode());
			mobileloginCall.setData(mobileloginBean);
			mobileloginCall.callService();			
			Logger.verifyEquals("400", mobileloginCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_MobileLogin_009() {
		Logger.start(true,"不输入Did失败");
		try {
			String moblie = TestDataManager.getPhoneNum();
			if (JUserCallService.mobileExists(moblie)) {
				moblie = TestDataManager.getPhoneNum();
			}
			mobileloginBean.setAppSource(AppSourceEnum.GlobalApp.getCode());
			mobileloginBean.setClientIp(TestDataManager.getRandomIp());
			mobileloginBean.setDeviceId(TestDataManager.getRandomUUID().toString());
//			mobileloginBean.setDid(TestDataManager.getRandomUUID().toString());
			mobileloginBean.setMobile(moblie);
			mobileloginBean.setTerminalSource(TerminalSourceEnum.IOS.getCode());
			mobileloginCall.setData(mobileloginBean);
			mobileloginCall.callService();			
			Logger.verifyEquals("400", mobileloginCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_MobileLogin_010() {
		Logger.start(true,"不输入TerminalSource失败");
		try {
			String moblie = TestDataManager.getPhoneNum();
			if (JUserCallService.mobileExists(moblie)) {
				moblie = TestDataManager.getPhoneNum();
			}
			mobileloginBean.setAppSource(AppSourceEnum.GlobalApp.getCode());
			mobileloginBean.setClientIp(TestDataManager.getRandomIp());
			mobileloginBean.setDeviceId(TestDataManager.getRandomUUID().toString());
			mobileloginBean.setDid(TestDataManager.getRandomUUID().toString());
			mobileloginBean.setMobile(moblie);
//			mobileloginBean.setTerminalSource(TerminalSourceEnum.IOS.getCode());
			mobileloginCall.setData(mobileloginBean);
			mobileloginCall.callService();			
			Logger.verifyEquals("400", mobileloginCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_MobileLogin_011() {
		Logger.start(true,"锁定用户登入失败");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			JUserCallService.lockUsers(userId);
			mobileloginBean.setAppSource(AppSourceEnum.GlobalApp.getCode());
			mobileloginBean.setClientIp(TestDataManager.getRandomIp());
			mobileloginBean.setDeviceId(TestDataManager.getRandomUUID().toString());
			mobileloginBean.setDid(TestDataManager.getRandomUUID().toString());
			mobileloginBean.setMobile(regbean.getLoginMobile());
			mobileloginBean.setTerminalSource(TerminalSourceEnum.IOS.getCode());
			mobileloginCall.setData(mobileloginBean);
			mobileloginCall.callService();			
			Logger.verifyEquals("6", mobileloginCall.getString("code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
}